package org.redmagic.service.controller.ui;

import javax.servlet.http.HttpServletRequest;

import org.redmagic.service.domain.ui.DisplayProperty;
import org.redmagic.service.ui.RedMagicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class EditPropertyController {

	private static final String REDIRECT_VIEW_ALL = "redirect:/viewAll";
	private static final String EDIT_PROPERTY_VIEW = "editPropertyForm";
	private static final String PROPERTY_FORM_ATTRIBUTE = "propertyForm";
	
	@Autowired
	private RedMagicService redMagicService;
	
	@RequestMapping(value="/editPropertyForm", method=RequestMethod.GET)
	public ModelAndView editPropertyForm(HttpServletRequest request) {
		String id = request.getParameter("id");
		DisplayProperty displayProperty = this.redMagicService.getPropertyById(id);
		return new ModelAndView(EDIT_PROPERTY_VIEW, PROPERTY_FORM_ATTRIBUTE, displayProperty);
	}
	
	@RequestMapping(value="/editProperty", method=RequestMethod.POST)
	public String edit(HttpServletRequest request, @ModelAttribute(PROPERTY_FORM_ATTRIBUTE) DisplayProperty displayProperty) {
		this.redMagicService.update(displayProperty);
		return REDIRECT_VIEW_ALL;
	}
}